#!/bin/bash
### BEGIN INIT INFO
# chkconfig: 123456 80 90
# Provides:        host_sys_init
# Required-Start:    $local_fs  $remote_fs
# Required-Stop:    $local_fs  $remote_fs
# Default-Start:    2 3 4 5
# Default-Stop:        0 1 6
# Short-Description:    Set up davinci servers
# Description:
### END INIT INFO
ASCEND_SECLOG="/var/log/ascend_seclog"
log_name="${ASCEND_SECLOG}/ascend_run_servers.log"
file_info_name="${ASCEND_SECLOG}/ascend_run_file.info"
installInfo="/etc/ascend_install.info"
[ ! -d "${ASCEND_SECLOG}" ] && mkdir -m 750 -p "${ASCEND_SECLOG}"
touch $log_name $file_info_name

getInstallParam() {
    local _key="$1"
    local _file="$2"
    local _param
    # update "Driver_Install_Type" "UserName" "UserGroup" "Driver_Install_Path_Param" "Driver_Install_For_All"
    install_info_key_array=("Driver_Install_Type" "UserName" "UserGroup" "Driver_Install_Path_Param" "Driver_Install_For_All")
    if [ ! -f "${_file}" ];then
        exit 1
    fi

    for key_param in "${install_info_key_array[@]}"; do
        if [ ${key_param} == ${_key} ]; then
            _param=`grep -r "${_key}=" "${_file}" | cut -d"=" -f2-`
            break;
        fi
    done
    echo "${_param}"
}

# read Driver_Install_Path_Param from installInfo
UserName=$(getInstallParam "UserName" "${installInfo}")
UserGroup=$(getInstallParam "UserGroup" "${installInfo}")
Driver_Install_For_All=$(getInstallParam "Driver_Install_For_All" "${installInfo}")
# read Driver_Install_Path_Param from installInfo
Driver_Install_Path_Param=$(getInstallParam "Driver_Install_Path_Param" "${installInfo}")

targetdir="${Driver_Install_Path_Param}"
targetdir="${targetdir%*/}"
username=$UserName
usergroup=$UserGroup
if [ "$username" = "" ]; then
    username=HwHiAiUser
    usergroup=HwHiAiUser
fi
davinci_wait_times=300

wait_device_init() {
    "${targetdir}"/driver/tools/upgrade-tool --mini_devices >> $log_name
    if [ $? -ne 0 ]; then
        echo "upgrade-tool get device count failed" >>$log_name
    fi
}

setFileChmod() {
    local option="$1"
    local mod="$2"
    local file_path="$3"

    if [ "${Driver_Install_For_All}" == "yes" ]; then
        local mod=${mod:0:-1}${mod: -2:1}
    fi

    chmod ${option} "${mod}" "${file_path}" >& /dev/null
    if [ $? -ne 0 ]; then
        echo "Error: ${mod} $file_path chmod failed!"
        exit 1
    fi
}

changeDavinciMode() {
    devinci_manager_flag=0
    devmm_svm_flag=0

    for i in $(seq 1 $davinci_wait_times)
    do
        if [ -e /dev/davinci_manager ] && [ $devinci_manager_flag -ne 1 ];then
            setFileChmod -f 660 /dev/davinci_manager
            chown -f $username:$usergroup /dev/davinci_manager
            devinci_manager_flag=1
        fi

        if [ -e /dev/devmm_svm ] && [ $devmm_svm_flag -ne 1 ];then
            setFileChmod -f 660 /dev/devmm_svm
            chown -f $username:$usergroup /dev/devmm_svm
            devmm_svm_flag=1
        fi
        if [ $devinci_manager_flag -eq 1 ] && [ $devmm_svm_flag -eq 1 ];then
            break
        fi
        sleep 1
    done

    wait_device_init
    for i in $(ls /dev/davinci*)
    do
         setFileChmod -f 660 $i
         chown -f $username:$usergroup $i
    done
    chown -f $username:$usergroup /dev/davinci*
}

startMode(){
    if [ -d "${Driver_Install_Path_Param}" ]; then
        setFileChmod -f 640 $log_name
    fi

    changeDavinciMode

    return 0
}

endMode(){
    if [ -d "${Driver_Install_Path_Param}" ]; then
        setFileChmod -f 440 $log_name
        setFileChmod -f 640 $file_info_name
    fi

    return 0
}

servers_stop () {
    return 0
}

hdc_file_check() {
    hdc_wait_times=300
    for i in $(seq 1 $hdc_wait_times)
    do
        if [ -e /dev/hisi_hdc ];then
            setFileChmod -f 660 /dev/hisi_hdc
            chown -f $username:$usergroup /dev/hisi_hdc
            break
        fi
        echo "hdcdrv is not ready, wait ($i/$hdc_wait_times)..." >>$log_name
        sleep 1
    done

    return 0
}

clean_servers_log() {
    echo > $log_name
    return 0
}

servers_log() {
    dmesg >> $log_name
    lsmod >> $log_name
    ps -ef >>$log_name

    return 0
}

run_file_info() {
    ls -lR "$1" >$file_info_name
    return 0
}

set_sock_option() {
    SLOGD_SIZE_16M=16777216
    SLOGD_SIZE_2M=2097152
    SLOGD_DGRAM_QLEN=512000
    sysctl -w net.core.wmem_max=$SLOGD_SIZE_2M > /dev/null 2>&1
    sysctl -w net.core.rmem_max=$SLOGD_SIZE_16M > /dev/null 2>&1
    sysctl -w net.unix.max_dgram_qlen=$SLOGD_DGRAM_QLEN > /dev/null 2>&1

    return 0
}

servers_start() {
    clean_servers_log
    if [ -d "$targetdir"/driver/tools ];then
        hdc_file_check
        set_sock_option

        sleep 0.1
    fi

    run_file_info "$targetdir"
    servers_log
    return 0
}

case "$1" in
    start)
    startMode
    servers_start
    ;;

    stop)
    ;;

    restart)
    startMode
    servers_stop
    servers_start
    ;;

    *)
    # no-op
    echo "[ERROR]The arguement doesn't support" >> $log_name
    ;;
esac
endMode

exit 0
